package sample.clients;



import org.hibernate.HibernateException;
import org.hibernate.Query;

import sample.dao.DAO;
import sample.entity.Advert;
import sample.entity.Category;

public class GetAdvert  extends DAO{
	
	public static void main(String[] args) throws Exception {
		
		GetAdvert self=new GetAdvert();
		
		self.getAdvert(1);
		
	}
	
	public void getAdvert(long id) throws Exception{
		
		try{
			begin();
			
			Query advertQuery=getSession().createQuery("from Advert where id= :id");
			advertQuery.setString("id", id+"");
			
			Advert advert=(Advert)advertQuery.uniqueResult();
			
			System.out.println(advert.getId() +" , title: "+advert.getTitle());
			
			System.out.println("Adverts in category :");
			for(Category item :advert.getCategories()){
				System.out.println(item.getTitle());
			}
			commit();
			
		}
		catch(HibernateException e){
			throw new Exception(e.getMessage());
		}
		
	}
}
